<?php
namespace plugins\member_login\controller;
use RongCloud\RongCloud;
use think\Db;
use plugins\member_login\validate\MemberValidate;
use plugins\member\model\MemberModel;
use cmf\controller\ApiThirdAccessBaseController;
use plugins\member\interfaceLib\MemberClassify;
use plugins\finance\model\FinanceModel;

define("APPKEY", 'p5tvi9dspe834');
define('APPSECRET', 'b8fHKCCpFMNe0N');

/**
 * 手机号码控制器
 */
class ApiPhonePassController extends ApiThirdAccessBaseController
{
	//手机验证码登陆
	public function login($param)
	{
        require 'RongCloud/RongCloud.php';
		//验证参数
		// $result = $this->validate( $param['data'] , 'Api.phoneLogin' );
		$checkParam['phone'] = ['name'=>'手机号码','type'=>'require|regex:/^1[34578]\d{9}$/','message'=>['regex'=>'请输入正确的手机号码']];
		$checkParam['password'] = ['name'=>'密码'];
		checkParam($checkParam,$param['data']);
		//检测
		//没有直接注册    phone code
		$check_member = ( new MemberModel )->get(['phone'=>$param['data']['phone']]);
		//该手机没有注册
		if($check_member == null){
            return api_json_echo( false , '该用户未注册' );
		}
        $check = Db::name('member')->where('phone',$param['data']['phone'])->where('password',cmf_password($param['data']['password']))->find();
		if($check['status'] == 1){

            $diviceid = '';    //设备号
            $cid = '';    //设备号
            if(!empty($param['diviceid'])){
                $diviceid = $param['diviceid'];
            }
            if(!empty($param['cid'])){
                $cid = $param['cid'];
            }

			//修改登陆时间与ip
			$Member = ( new MemberModel )->get([ 'phone' => $param['data']['phone'] , 'status' => 1 ]);
			$result = $Member->allowField(true)->save([ 'last_date' => time() , 'last_ip' => $this->request->ip(),'diviceid'=>$diviceid,'cid'=>$cid ]);
			if($result){
				//生成token
				$data['uid'] = $check['uid'];
				$data['token'] = $this->setIdentity($check['uid']);
				$data['refresh_token'] = $this->setRefreshToken($check['uid']);
                $data['aes'] = $check['uid_aes'];


				if(empty($check['token'])){
                    $RongSDK = new RongCloud(APPKEY, APPSECRET);
                    $user = [
                        'id' => $check['uid'],
                        'name' => $check['nick_name'],//用户名称
                        'portrait' => ZY_APP_PATH.$check['head_portrait'] //用户头像
                    ];
                    $register = $RongSDK->getUser()->register($user);
                    if($register['code'] == 200){
                        Db::name('member')->where('uid',$check['uid'])->update([
                            'token'=>$register['token']
                        ]);
                    }
                }

				return api_json_echo( true , '会员登录成功' , $data );
			}else{
				return api_json_echo( false , '会员登录失败，请检查号码' );
			}
		}else if($check['status'] == 2){
			return api_json_echo( false , '该用户被禁用，请重试' );
		}else if($check['status'] == 3){
            return api_json_echo( false , '该用户已失效，请重试' );
		}
		return api_json_echo( false , '会员登录失败，请检查号码' );
	}
}